<?php


namespace App\Http\Controllers\User;


use App\Http\Controllers\BaseController;
use App\Models\Client;
use App\Models\Event;
use App\Models\EventEmployee;
use App\Models\User;
use Illuminate\Http\Request;

class EventController extends BaseController
{
    public function  getEvent(Request $request){
        //获取user_id
        $user_id = $request->cache_data['id'];

        $event = EventEmployee::where('user_id',$user_id)
            ->join('events','events.id','event_employees.event_id')
            ->get(['events.id','events.name','events.type','events.address','events.date','events.permission','events.status'])
            ->toArray();

        //如果无数据返回''
        if (empty($event)) return $this->returnJson(BaseController::OK,'success','');

        return $this->returnJson(BaseController::OK,'success',$event);
    }

    public function  getIdEvent(Request $request,$event_id){
        $user_id = $request->cache_data['id'];

        //判断必填参数是否为空
        if (empty($event_id)){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        //获取员工信息
        $employee = EventEmployee::where('event_id',$event_id)
            ->join('users','users.id','event_employees.user_id');

        //判断该用户是否和此订单员工关联
        if (!in_array($user_id,$employee->pluck('users.id')->toArray())) return $this->returnJson(BaseController::NOTFOUND,'not found','');

        $data =  Event::where('id',$event_id)
            ->with(['client'])
            ->first(['events.id','events.name','events.client_id','events.type','events.photo','events.address','events.date','events.permission', 'events.status']);

        $data['employees'] = $employee ->get(['users.id','users.email','users.photo','users.scope','users.firstname','users.lastname','status']);

        return $this->returnJson(BaseController::OK,'success',$data);
    }
}
